package model;

import ecom.beans.AccountBean;
import ecom.beans.EcomAdminRemote;
import ecom.beans.EcomCustomerRemote;
import ecom.shell.ShellRemote;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

public class Init extends AbstractPage {

    public String welcome() throws Exception {
        //INITIALISATION
        Context initialContext = null;
        UserTransaction utx = null;
        InitialContext ic = new InitialContext();
        utx = (UserTransaction) ic.lookup("UserTransaction");
        EcomAdminRemote ecomAdminBean = (EcomAdminRemote) ic.lookup(EcomAdminRemote.class.getName());
        EcomCustomerRemote ecomCustomerBean = (EcomCustomerRemote) ic.lookup(EcomCustomerRemote.class.getName());
        ShellRemote shell = (ShellRemote) ic.lookup(ShellRemote.class.getName());
        shell.load(ecomCustomerBean, ecomAdminBean, true);

        this.getSession().put("shell", shell);
        this.getSession().put("adminRemote", ecomAdminBean);
        this.getSession().put("customerRemote", ecomCustomerBean);
        this.getSession().put("admin", false);

        return "home";
    }

    public String currency() throws Exception {
        //RECUPERATION DU SHELL
        ShellRemote shell = (ShellRemote) getSession().get("shell");

        String[] currency = (String[]) this.getParameters().get("currency");

        if (currency != null && !"".equalsIgnoreCase(currency[0])) {
            shell.executeCommand("currency " + currency[0]);
        }
        return "redirectToReferrer";
    }

    @Override
    public String execute() {
        this.setPage("welcome.jsp");
        return SUCCESS;
    }
}